/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.apt.Bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apt.connectDB.MyConnect;

/**
 *
 * @author MinhThang
 */
public class active_payment_lecture extends org.apache.struts.action.ActionForm {
    private String txtID;
    private String txtEventID;
    private String txtCurrentJoin;
    public active_payment_lecture() {
        super();
        // TODO Auto-generated constructor stub
    }

    public boolean ActivePayment(int ID,String user){

        Connection conn=null;
        PreparedStatement ps =null;
        //SQL command  Update PaymentLecture
        String sql="UPDATE PaymentLecture SET StatusPayment = ? WHERE (ID = ?)";
        //SQL command  Insert data to PaymentLectureAdmin table
        String sql2="INSERT INTO [dbo].[PaymentLectureAdmin] ([PaymentLectureID], [AdminID], [Status]) VALUES (?, ?, ?)";
       //SQL command Update data to LectureEvent table
        String sql3="UPDATE LectureEvent SET CurrentJoin = ? WHERE  (EventID = ?)";
        try {
            conn=MyConnect.getConnection();
            //update payment lecture
            ps=conn.prepareCall(sql);
            ps.setBoolean(1,true);
            ps.setInt(2,ID);
            ps.executeUpdate();
            //insert to Admin payment
            ps=conn.prepareCall(sql2);
            ps.setInt(1,ID);
            ps.setString(2, user);
            ps.setBoolean(3, true);
            ps.executeUpdate();
            //update current
            ps=conn.prepareCall(sql3);
            int current=Integer.parseInt(getTxtCurrentJoin());
            current++;
            ps.setInt(1, current);
            ps.setInt(2, Integer.parseInt(getTxtEventID()));
            ps.executeUpdate();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (getTxtID() == null || getTxtID().length() < 1) {
            errors.add("name", new ActionMessage("error.name.required"));
            // TODO: add 'error.name.required' key to your resources
        }
        return errors;
    }

    /**
     * @return the txtID
     */
    public String getTxtID() {
        return txtID;
    }

    /**
     * @param txtID the txtID to set
     */
    public void setTxtID(String txtID) {
        this.txtID = txtID;
    }

    /**
     * @return the txtEventID
     */
    public String getTxtEventID() {
        return txtEventID;
    }

    /**
     * @param txtEventID the txtEventID to set
     */
    public void setTxtEventID(String txtEventID) {
        this.txtEventID = txtEventID;
    }

    /**
     * @return the txtCurrentJoin
     */
    public String getTxtCurrentJoin() {
        return txtCurrentJoin;
    }

    /**
     * @param txtCurrentJoin the txtCurrentJoin to set
     */
    public void setTxtCurrentJoin(String txtCurrentJoin) {
        this.txtCurrentJoin = txtCurrentJoin;
    }
}
